package org.example.test;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.example.utils.POIUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class PoiDemo1 {

    private static final String filepath = "D:\\poifile\\test\\";
    public static void main(String[] args) throws IOException {

        test2();
    }


    static void test1() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filepath + "poiexcel03.xls");

        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        //获取第一行
        Row row1 = sheet.getRow(0);
        //获取第一行的第一个单元格
        Cell cell11 = row1.getCell(0);
        //获取第一行第二个单元格
        Cell cell12 = row1.getCell(1);
        System.out.println("第一行第一个单元格的内容是："+cell11.getStringCellValue());
        System.out.println("第一行第二个单元格的内容是："+cell12.getStringCellValue());

        //获取第一行
        Row row2 = sheet.getRow(1);
        //获取第一行的第一个单元格
        Cell cell21 = row2.getCell(0);
        //获取第一行第二个单元格
        Cell cell22 = row2.getCell(1);
        System.out.println("第一行第一个单元格的内容是："+cell21.getStringCellValue());
        System.out.println("第一行第二个单元格的内容是："+cell22.getStringCellValue());
    }


    static void test2() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(filepath + "poiexcel03.xls");

        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);

        POIUtils.RowAndColumnFromCellReference r1 = POIUtils.getRowAndColumnFromCellReference("A1");
        System.out.println(r1.toString());

        String v1 =POIUtils.getCellValue(POIUtils.getCell(sheet,r1.getRowNumber(),r1.getColumnNumber())) ;
        System.out.println(v1);


        POIUtils.RowAndColumnFromCellReference r2 = POIUtils.getRowAndColumnFromCellReference("A2");
        System.out.println(r2.toString());

        String v2 =POIUtils.getCellValue(POIUtils.getCell(sheet,r2.getRowNumber(),r2.getColumnNumber())) ;
        System.out.println(v2);

        POIUtils.RowAndColumnFromCellReference r3 = POIUtils.getRowAndColumnFromCellReference("B1");
        String v3 =POIUtils.getCellValue(POIUtils.getCell(sheet,r3.getRowNumber(),r3.getColumnNumber())) ;
        System.out.println(v3);


        int startRow = 1;
        int startCol = 2;
        int endRow = 1;
        int endCol = 3;

        Cell mergedCell = POIUtils.getMergedCell(sheet,startRow,startCol,endRow,endCol);
        String value = POIUtils.getCellValue(mergedCell);
        System.out.println(value);
    }
}
